package com.hymjweb.demo.hymj.basic_data_manage.xt_pick_up_point.mappper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.basic_data_manage.xt_pick_up_point.bean.XtPickUpPointPojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface XtPickUpPointMapper {

    class XtPickUpPointMapperProvider{

        public String findAll(Map map) {

            String sql= new SQL() {{
                SELECT("xd.* ");
                FROM("xt_pick_up_point xd ");
                WHERE("1=1 ");
            }}.toString();

            if (map != null) {
                String keyName = DataTypeUtil.toStringObject(map.get("keyName"));
                Long keyPickUpTypeId = DataTypeUtil.toLongObject(map.get("keyPickUpTypeId"));
                String keyCityId = DataTypeUtil.toStringObject(map.get("keyCityId"));

                if(keyName!=null){
                    //提货点名称、电话或地址
                    sql += " and (xd.name like " + DataTypeUtil.quoteAllLikeStr(keyName) + " or xd.phone like " + DataTypeUtil.quoteAllLikeStr(keyName) + " or xd.address like " + DataTypeUtil.quoteAllLikeStr(keyName) + ")";
                }
                if(keyPickUpTypeId!=null){
                    sql += " and xd.pick_up_type_id = " + keyPickUpTypeId;
                }
                if(keyCityId!=null){
                    sql += " and xd.city_id = " + DataTypeUtil.quoteStr(keyCityId);
                }

            }
            return  sql;
        }

        public String getXtPickUpPointPojoListByIds(String ids){
            String sql= new SQL() {{
                SELECT("xd.* ");
                FROM("xt_pick_up_point xd ");
                WHERE("1=1 and xd.id in (" + ids + ")");
            }}.toString();
            return sql;
        }

        public String queryPickUpPointByCondition(String key, String cityId) {

            String sql= new SQL() {{
                SELECT("xd.id,xd.pick_up_type_id, xd.city_id, xd.name,xd.contact, xd.phone, xd.address, xd.file_id, xd.comment");
                FROM("xt_pick_up_point xd where 1 = 1 ");
            }}.toString();

            if(!DataTypeUtil.isEmptyStr(key)){
                sql += " and xd.name like " + DataTypeUtil.quoteAllLikeStr(key);
            }

            if(!DataTypeUtil.isEmptyStr(cityId)){
                sql += " and xd.city_id = " + DataTypeUtil.quoteStr(cityId);
            }

            return  sql;
        }


        public String insert(XtPickUpPointPojo pojo) {
            String sql = new SQL() {{
                INSERT_INTO("xt_pick_up_point");
                VALUES("id","#{id}");
                VALUES("pick_up_type_id","#{pickUpTypeId}");
                VALUES("city_id","#{cityId}");
                VALUES("name","#{name}");
                VALUES("phone","#{phone}");
                VALUES("address","#{address}");

                VALUES("contact","#{contact}");
                VALUES("contact_phone","#{contactPhone}");
                VALUES("cw_contact_name","#{cwContactName}");
                VALUES("cw_contact_phone","#{cwContactPhone}");

                VALUES("file_id","#{fileId}");
                VALUES("comment","#{comment}");
                VALUES("create_time", "#{createTime}");
                VALUES("create_user_id", "#{createUserId}");
                VALUES("update_time", "#{updateTime}");
                VALUES("update_user_id", "#{updateUserId}");
            }}.toString();
            return sql;
        }

        public String update(XtPickUpPointPojo pojo) {
            String sql= new SQL() {{
                UPDATE("xt_pick_up_point xd");
                SET("xd.id = #{id}");
                SET("xd.pick_up_type_id = #{pickUpTypeId}");
                SET("xd.city_id = #{cityId}");
                SET("xd.name = #{name}");
                SET("xd.phone = #{phone}");
                SET("xd.address = #{address}");

                SET("xd.contact = #{contact}");
                SET("xd.contact_phone = #{contactPhone}");
                SET("xd.cw_contact_name = #{cwContactName}");
                SET("xd.cw_contact_phone = #{cwContactPhone}");

                SET("xd.file_id = #{fileId}");
                SET("xd.comment = #{comment}");
                SET("xd.update_time = #{updateTime}");
                SET("xd.update_user_id = #{updateUserId}");
                WHERE("xd.id=#{id}");
            }}.toString();
            return sql;
        }

        public String delete(String ids) {
            String sql = new SQL() {{
                DELETE_FROM("xt_pick_up_point");
                WHERE("id in ( " + ids  + ")");
            }}.toString();
            return sql;
        }

        // UK_XT_PICK_UP_POINT (pick_up_type_id, city_id, name)
        public String queryPojoByUk(XtPickUpPointPojo pointPojo) {
            String sql= new SQL() {{
                SELECT("t.*");
                FROM("xt_pick_up_point t");
                WHERE(" t.name = #{name}");
                WHERE(" t.pick_up_type_id = #{pickUpTypeId}");
                WHERE(" t.city_id = #{cityId}");
            }}.toString();
            return  sql;
        }
    }


    @Results(id = "xtPickUpPointMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "pick_up_type_id", property = "pickUpTypeId"),
            @Result(column = "city_id", property = "cityId"),
            @Result(column = "name", property = "name"),
            @Result(column = "phone", property = "phone"),
            @Result(column = "address", property = "address"),
            @Result(column = "contact", property = "contact"),
            @Result(column = "contact_phone", property = "contactPhone"),
            @Result(column = "cw_contact_name", property = "cwContactName"),
            @Result(column = "cw_contact_phone", property = "cwContactPhone"),
            @Result(column = "file_id", property = "fileId"),
            @Result(column = "comment", property = "comment"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "create_user_id", property = "createUserId"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "update_user_id", property = "updateUserId")
    })
    @SelectProvider(type = XtPickUpPointMapperProvider.class, method = "findAll")
    List<XtPickUpPointPojo> findAll(Map map);

    @ResultMap("xtPickUpPointMapId")
    @SelectProvider(type = XtPickUpPointMapperProvider.class, method = "getXtPickUpPointPojoListByIds")
    List<XtPickUpPointPojo> getXtPickUpPointPojoListByIds(String ids);


    @ResultMap("xtPickUpPointMapId")
    @SelectProvider(type = XtPickUpPointMapperProvider.class, method = "queryPickUpPointByCondition")
    List<XtPickUpPointPojo> queryPickUpPointByCondition(String key, String cityId);

    @ResultMap(("xtPickUpPointMapId"))
    @SelectProvider(type = XtPickUpPointMapperProvider.class, method = "queryPojoByUk")
    XtPickUpPointPojo queryPojoByUk(XtPickUpPointPojo pointPojo);


    @InsertProvider(type = XtPickUpPointMapperProvider.class, method = "insert")
    int insert(XtPickUpPointPojo pojo);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @UpdateProvider(type = XtPickUpPointMapperProvider.class, method = "update")
    int update(XtPickUpPointPojo obj);

    @DeleteProvider(type = XtPickUpPointMapperProvider.class, method = "delete")
    int delete(String ids);

}
